
package com.foxmail.aroundme.boring.weight.header;


import android.app.Activity;
import android.content.Context;
import android.support.annotation.DrawableRes;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.TypefaceSpan;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.foxmail.aroundme.boring.R;
import com.foxmail.aroundme.boring.app.App;
import com.foxmail.aroundme.boring.manager.SettingManager;
import com.foxmail.aroundme.boring.utils.util.KeyBoardUtils;
import com.foxmail.aroundme.boring.utils.util.ViewUtil;

public final class Header implements TextWatcher, TextView.OnEditorActionListener {
    private View mRoot;

    private ImageView mLeftBtn;

    private ImageView mLogo;

    private ImageButton mRightButton;

    private ImageButton mTwoRightButton;

    private TextView mTitle;

    private TextView mPhotoIndex;

    private OnClickListener mLeftListener;

    private OnClickListener mRightClickListener;

    private OnClickListener mTwoRightClickListener;

    private OnClickListener mTestClickListener;

    private SearchAfterTextChanged mSearchChange;

    private boolean isLeftClick = true;
    private boolean isRigthClick = true;

    public static Header create(Activity activity, int titleId) {
        return new Header(activity, titleId);
    }

    private Header(Activity activity, int titleId) {
        mRoot = activity.findViewById(titleId);
        mLeftBtn = (ImageView) mRoot.findViewById(R.id.leftButton);
        View left = mRoot.findViewById(R.id.left);
        left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLeftListener != null && isLeftClick) {
                    mLeftListener.onClick(v);
                }
            }
        });
        mLeftBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLeftListener != null) {
                    mLeftListener.onClick(v);
                }
            }
        });
        mLogo = (ImageView) mRoot.findViewById(R.id.logo);
        mTitle = (TextView) mRoot.findViewById(R.id.title);

        if (mTitle != null) {
            mTitle.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mTestClickListener != null) {
                        mTestClickListener.onClick(v);
                    }
                }
            });
        }


        mRightButton = (ImageButton) mRoot.findViewById(R.id.rightButton);
        View right = mRoot.findViewById(R.id.right);
        right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRightClickListener != null && isRigthClick) {
                    mRightClickListener.onClick(v);
                }
            }
        });
        mRightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRightClickListener != null) {
                    mRightClickListener.onClick(v);
                }
            }
        });

        mTwoRightButton = (ImageButton) mRoot.findViewById(R.id.right_button_two);
        View twoRight = mRoot.findViewById(R.id.right_two);
        twoRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTwoRightClickListener != null) {
                    mTwoRightClickListener.onClick(v);
                }
            }
        });

        mTwoRightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTwoRightClickListener != null) {
                    mTwoRightClickListener.onClick(v);
                }
            }
        });
        mPhotoIndex = (TextView) mRoot.findViewById(R.id.photo_index);
    }


    public Header setLogo(int imageId) {
        if (mLogo == null) {
            return this;
        }
        if (imageId > 0) {
            mLogo.setVisibility(View.VISIBLE);
            mLogo.setBackgroundResource(imageId);
        } else {
            mLogo.setVisibility(View.GONE);
        }
        return this;
    }

    public Header setLeftButton(int logoResId) {
        return setLeftButton(logoResId, null);
    }

    public Header setLeftButton(int logoResId, OnClickListener listener) {
        if (logoResId > 0) {
            mLeftBtn.setVisibility(View.VISIBLE);
            mLeftBtn.setImageResource(logoResId);
            mLeftListener = listener;
        } else {
            mLeftBtn.setVisibility(View.INVISIBLE);
        }
        return this;
    }

    public Header setRightButton(int resid, OnClickListener listener) {
        if (resid > 0) {
            mRightButton.setImageResource(resid);
            mRightButton.setVisibility(View.VISIBLE);
            mRightClickListener = listener;
        } else {
            mRightButton.setVisibility(View.INVISIBLE);
        }
        return this;
    }

    public Header setTwoRightButton(int resId, OnClickListener listener) {
        if (resId > 0) {
            mTwoRightButton.setImageResource(resId);
            mTwoRightButton.setVisibility(View.VISIBLE);
            mTwoRightClickListener = listener;
        } else {
            mTwoRightButton.setVisibility(View.INVISIBLE);
        }
        return this;
    }

    public Header setTitle(int titleId) {
        if (mTitle == null) {
            return this;
        }
        if (titleId > 0) {
            mTitle.setText(titleId);
            mTitle.setVisibility(View.VISIBLE);
        } else {
            mTitle.setVisibility(View.GONE);
        }
        return this;
    }

    public Header setTitle(String title) {
        if (mTitle == null) {
            return this;
        }
        if (!TextUtils.isEmpty(title)) {
            mTitle.setText(title);
            mTitle.setVisibility(View.VISIBLE);
        } else {
            mTitle.setVisibility(View.GONE);
        }
        return this;
    }

    public Header showHideRightButton(boolean isShow) {
        if (mRightButton != null && mTwoRightButton != null) {
            if (isShow) {
                mTwoRightButton.setVisibility(View.VISIBLE);
                mRightButton.setVisibility(View.VISIBLE);
            } else {
                mTwoRightButton.setVisibility(View.INVISIBLE);
                mRightButton.setVisibility(View.INVISIBLE);
            }
        }
        return this;
    }


    public void setRigthClick(boolean rigthClick) {
        isRigthClick = rigthClick;
    }

    public void setTestClick(OnClickListener testClickListener) {
        mTestClickListener = testClickListener;
    }



    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }


    public void setSearchChange(SearchAfterTextChanged searchChange) {
        this.mSearchChange = searchChange;
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH && mSearchChange != null) {
            mSearchChange.onSearch();
        }
        return false;
    }

    public interface SearchAfterTextChanged {
        void afterTextChanged(Editable s);

        void onSearch();
    }

    //设置图片指示器
    public void setPhotoIndex(String index, String count) {
        if (mPhotoIndex == null || TextUtils.isEmpty(index) || TextUtils.isEmpty(count)) {
            return;
        }

        if (mPhotoIndex.getVisibility() != View.VISIBLE) {
            mPhotoIndex.setVisibility(View.VISIBLE);
        }

        SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
        stringBuilder.append(index);
        stringBuilder.append("/");
        stringBuilder.append(count);

        int indexLen = index.length();
        AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(ViewUtil.spToPx(App.getContext(), 20));
        TypefaceSpan typefaceSpan = new TypefaceSpan("sans-serif");

        stringBuilder.setSpan(absoluteSizeSpan, 0, indexLen, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        stringBuilder.setSpan(typefaceSpan, 0, indexLen, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        mPhotoIndex.setText(stringBuilder);
    }

    //设置背景
    public void setBackground(@DrawableRes int id) {
        if (mRoot != null) {
            mRoot.setBackgroundResource(id);
        }
    }
}
